/**
 * Created by zhangwj on 2017/1/11.
 */
function DemoUpdateController(Demo,$uibModal,$scope,$state,$stateParams){
    var vm=this
    vm.user = {user_name:$stateParams.user_name}
    vm.user.id = $stateParams.id

    vm.add = function() {
        Demo.add(vm.user).$promise.then(function(response){
            //处理add之后的返回结果
            if(response.success != null) {
                // 模拟窗口打开之后执行该函数
                var modalInstance = $uibModal.open({
                    templateUrl: 'myModalContent.html',
                    controller: 'ModalInstanceCtrl',
                    size: Demo
                });

                // 模拟窗口关闭后回传调用
                modalInstance.result.then(function () {
                    $state.go('userList', vm.queryParams)
                });
            } else {
                $scope.serverError = response.fail
            }
        })
    }

    vm.cancel = function() {
        $state.go('userList', vm.queryParams);
    }

    //验证重复密码是否相同
    $scope.$watch('vm.user', function (newValue, oldValue) {
        if (newValue) {
            if (newValue.pass_word_reply) {
                if (!_.isEqual(newValue.pass_word_reply, newValue.pass_word)) {
                    vm.pwdInvalid = true
                    vm.form.pass_word_reply.$invalid = true
                    vm.form.$invalid = true
                } else {
                    vm.pwdInvalid = false
                    vm.form.pass_word_reply.$invalid = false
                }
            }
        }
    }, true)
}
